package com.ctg.payroll2.service.social.impl;

import java.sql.SQLException;
import java.util.List;

import com.ctg.payroll2.bean.filter.social.CsSocialFilter;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.bean.po.social.CsSocial;
import com.ctg.payroll2.bean.po.social.CsTemplateSocialItemSearchBean;
import com.ctg.payroll2.dao.social.CustomTemplateDao;
import com.ctg.payroll2.service.BaseService;
import com.ctg.payroll2.service.social.CsSocialService;
import com.ctg.platform.util.Page;
/**
 * 客户社保模版service实现
 * @author 鲁力 2009-9-7 下午03:19:15
 *
 */
@SuppressWarnings("unchecked")
public class CsSocialServiceImpl extends BaseService implements CsSocialService {
	private CustomTemplateDao dao;

	public CsSocial getCsSocialById(Long id) {
		if (id==null) {
			return null;
		}
		CsSocial entity = new CsSocial();
		List<CsSocial> list = dao.getCsSocialByTemplateId(id);
		if(list.size()==1){
			entity = list.get(0);
			entity.setBeanList(dao.getCsSocialDetailByTemplateId(entity.getTemplate_id()));
		}
		return entity;
	}

	public void insertCsSocial(CsSocial csSocial, SysUser user) {
		csSocial.setCompany_code(user.getCompanyCode());
		csSocial.setCreate_code(user.getEmployeeCsCode());
		csSocial.setCreator_id(user.getEmployeeId());
		csSocial.setCreator_ip(user.getLoginIP());
		//主键
		Long templateId = dao.insertCsSocial(csSocial);
		
		List<CsTemplateSocialItemSearchBean> addfieds = csSocial.getAddfieds();
		if(addfieds.size()>0){
			for (CsTemplateSocialItemSearchBean detail : addfieds) {
				detail.setUserId(user.getEmployeeId());
				detail.setTemplateId(templateId);
				detail.setShowName(csSocial.getName());
				dao.insertCsSocialDetail(detail);
			}
		}
	}
	public Page queryCsSocial(CsSocialFilter filter) {
		return dao.queryCustomTemplateByFilter(filter);
	}
	
	public List getPbSocialItemByFilter(String templateId){
		return dao.getPbSocialItemByFilter(templateId);
	}
	public void deleteCsSocialByIds(String ids) throws SQLException{
		dao.deleteCsSocialByIds(ids);
		dao.deleteCsSocialDetailByIds(ids);
	}
	public void updateCsSocial(CsSocial csSocial, SysUser user) {
		if (csSocial==null) {
			return;
		}
		csSocial.setCompany_code(user.getCompanyCode());
		csSocial.setCreate_code(user.getEmployeeCsCode());
		csSocial.setCreator_id(user.getEmployeeId());
		csSocial.setCreator_ip(user.getLoginIP());
		dao.updateCsSocial(csSocial);
		List<CsTemplateSocialItemSearchBean> addfieds = csSocial.getAddfieds();
		List<CsTemplateSocialItemSearchBean> modifieds = csSocial.getModifieds();
		List<CsTemplateSocialItemSearchBean> deleteds = csSocial.getDeleteds();
		//处理新增的
		if(addfieds.size()>0){
			for (CsTemplateSocialItemSearchBean detail : addfieds) {
				detail.setUserId(user.getEmployeeId());
				detail.setTemplateId(csSocial.getTemplate_id());
				detail.setShowName(csSocial.getName());
				dao.insertCsSocialDetail(detail);
			}
		}
		//处理修改的
		if(modifieds.size()>0){
			for (CsTemplateSocialItemSearchBean detail : modifieds) {
				detail.setUserId(user.getEmployeeId());
				dao.updateCsSocialDetail(detail);
			}
		}
		//处理删除的
		if(deleteds.size()>0){
			StringBuffer sb = new StringBuffer();
			for (CsTemplateSocialItemSearchBean detail : deleteds) {
				sb.append(detail.getSubSocialSecurityId()).append(",");
			}
			try {
				dao.deleteCsSocialDetailByIds(sb.substring(0,sb.length()-1));
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * @param dao 设置dao
	 */
	public void setDao(CustomTemplateDao dao) {
		this.dao = dao;
	}
}
